package com.csthink.web.infrastructure.auditlog;

import com.csthink.auditlog.config.builder.BaseAuditLogBuilder;
import com.csthink.common.infrastructure.enums.Source;
import com.csthink.mysql.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.function.Supplier;

/**
 * @author <a href="mailto:security.2009@live.cn">Mars</a>
 * @since 2023-09-12
 */
public class WebAuditLogBuilder extends BaseAuditLogBuilder {

    @Configuration
    public static class Config {
        @Bean
        public Supplier<BaseAuditLogBuilder> builderFactory() {
            return WebAuditLogBuilder::create;
        }
    }

    private WebAuditLogBuilder() {}

    public static WebAuditLogBuilder create() {
        return new WebAuditLogBuilder();
    }

    @Override
    protected void handleAccountInfo() {
        // If set by other way, use that
        if (StringUtils.isNotBlank(this.auditLog.getAccountId())) {
            return;
        }
        // todo spring security
//        this.auditLog.setAccountId(AuthenticationInfoUtil.getAccountId());

    }

    @Override
    protected void handleOperatorInfo() {
        // todo spring security
//        User user = AuthenticationInfoUtil.getLoginUser();
        User user = new User();
        if (user != null) {
            this.auditLog.setOperatorId(user.getId());
            this.auditLog.setOperatorInfo(user.getEmail());
        }
    }

    @Override
    protected void handleSource() {
        this.auditLog.setSource(Source.CST_WEB.name());
    }
}
